package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    RoomAttrValueMapper attrValueMapper;

    @Autowired
    RoomFacilityMapper facilityMapper;

    @Autowired
    RoomLabelMapper labelMapper;

    @Autowired
    RoomPaymentTypeMapper paymentTypeMapper;

    @Autowired
    RoomLeaseTermMapper leaseTermMapper;

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    ApartmentInfoService apartmentInfoService;

    @Autowired
    RoomAttrValueService roomAttrValueService;

    @Autowired
    RoomFacilityService roomFacilityService;

    @Autowired
    RoomLabelService roomLabelService;

    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    RoomLeaseTermService roomLeaseTermService;

    @Override
    public IPage<RoomItemVo> pageItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItemByQuery(page,queryVo);
    }

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {

        super.saveOrUpdate(roomSubmitVo);

        Long roomId = roomSubmitVo.getId();

        //修改
        if (roomSubmitVo.getId()!= 0){
            //删除图片信息
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getItemId, roomId);
            lambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
            graphInfoService.remove(lambdaQueryWrapper);

            //删除属性列表信息
            LambdaQueryWrapper<RoomAttrValue> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(RoomAttrValue::getRoomId,roomId);
            roomAttrValueService.remove(lambdaQueryWrapper1);

            //删除配套信息列表
            LambdaQueryWrapper<RoomFacility> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(RoomFacility::getRoomId,roomId);
            roomFacilityService.remove(lambdaQueryWrapper2);

            //删除标签信息列表
            LambdaQueryWrapper<RoomLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomId);
            roomLabelService.remove(labelLambdaQueryWrapper);

            //删除支付方式列表
            LambdaQueryWrapper<RoomPaymentType> lambdaQueryWrapper3 =new LambdaQueryWrapper<>();
            lambdaQueryWrapper3.eq(RoomPaymentType::getRoomId,roomId);
            roomPaymentTypeService.remove(lambdaQueryWrapper3);

            //删除可选租期列表
            LambdaQueryWrapper<RoomLeaseTerm> lambdaQueryWrapper4 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper4.eq(RoomLeaseTerm::getRoomId,roomId);
            roomLeaseTermService.remove(lambdaQueryWrapper4);
        }


         //添加

        //添加图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList){
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(roomId);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //添加属性列表
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)){
            List<RoomAttrValue> roomAttrValues = new ArrayList<>();
            for (Long attrValueid : attrValueIds){
                RoomAttrValue roomAttrValue = new RoomAttrValue();
                roomAttrValue.setRoomId(roomId);
                roomAttrValue.setAttrValueId(attrValueid);
                roomAttrValues.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }

        //添加配套信息列表
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility> roomFacilities = new ArrayList<>();
            for (Long fid : facilityInfoIds){
                RoomFacility roomFacility = new RoomFacility();
                roomFacility.setFacilityId(fid);
                roomFacility.setRoomId(roomId);
                roomFacilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }

        //添加标签信息列表
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)){
            List<RoomLabel> roomLabels = new ArrayList<>();
            for (Long lid : labelInfoIds){
                RoomLabel roomLabel = new RoomLabel();
                roomLabel.setLabelId(lid);
                roomLabel.setRoomId(roomId);
                roomLabels.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabels);
        }

        //添加支付方式列表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            List<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
            for (Long pid : paymentTypeIds){
                RoomPaymentType roomPaymentType = new RoomPaymentType();
                roomPaymentType.setRoomId(roomId);
                roomPaymentType.setPaymentTypeId(pid);
                roomPaymentTypes.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }

        //添加可选租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            List<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long ltid : leaseTermIds){
                RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
                roomLeaseTerm.setRoomId(roomId);
                roomLeaseTerm.setLeaseTermId(ltid);
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }

    }

    @Override
    public void removeRoomById(Long id) {
        //删除图片信息
        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        lambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(lambdaQueryWrapper);

        //删除属性列表信息
        LambdaQueryWrapper<RoomAttrValue> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(lambdaQueryWrapper1);

        //删除配套信息列表
        LambdaQueryWrapper<RoomFacility> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(lambdaQueryWrapper2);

        //删除标签信息列表
        LambdaQueryWrapper<RoomLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(labelLambdaQueryWrapper);

        //删除支付方式列表
        LambdaQueryWrapper<RoomPaymentType> lambdaQueryWrapper3 =new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(lambdaQueryWrapper3);

        //删除可选租期列表
        LambdaQueryWrapper<RoomLeaseTerm> lambdaQueryWrapper4 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper4.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(lambdaQueryWrapper4);

        //删除房间信息
        boolean remove = this.removeById(id);
        if (!remove){
            throw new LeaseException(ResultCodeEnum.FAIL);
        }

    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        RoomInfo roomInfo = this.getById(id);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.getApartmentInfo(id);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        //图片
        List<GraphVo> graphVoList = graphInfoMapper.getGraphVoListByRid(id,ItemType.ROOM);
        roomDetailVo.setGraphVoList(graphVoList);
        //属性列表信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.getAttrValueVoList(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        //配套信息列表
        List<FacilityInfo> facilityInfos = facilityMapper.getFacilityInfos(id);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        //标签信息列表
        List<LabelInfo> labelInfos = labelMapper.getLabelInfosById(id);
        roomDetailVo.setLabelInfoList(labelInfos);
        //支付方式列表
        List<PaymentType> paymentTypes = paymentTypeMapper.getPaymentTypes(id);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        //可选租期列表
        List<LeaseTerm> leaseTerms = leaseTermMapper.getLeaseTerms(id);
        roomDetailVo.setLeaseTermList(leaseTerms);
        return roomDetailVo;
    }
}




